package com.lzb.compressmaster.activity;

import android.graphics.Color;
import android.text.TextUtils;
import android.view.View;

import com.blankj.utilcode.util.ToastUtils;
import com.lzb.compressmaster.BaseAc;
import com.lzb.compressmaster.Content;
import com.lzb.compressmaster.R;
import com.lzb.compressmaster.basic.utils.SPUtil;
import com.lzb.compressmaster.databinding.ActivitySetPwdBinding;
import com.lzb.compressmaster.dialog.SetAnswerDialog;

public class SetPwdActivity extends BaseAc<ActivitySetPwdBinding> implements View.OnClickListener {

    private String question1, question2, question3, answer1, answer2, answer3;

    @Override
    protected int onCreate() {
        return R.layout.activity_set_pwd;
    }

    @Override
    protected void initView() {
        mDataBinding.ivSetPwdBack.setOnClickListener(view -> getOnBackPressedDispatcher().onBackPressed());

        mDataBinding.tvQuestion1.setOnClickListener(this);
        mDataBinding.tvQuestion2.setOnClickListener(this);
        mDataBinding.tvQuestion3.setOnClickListener(this);
        mDataBinding.tvConfirm.setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        int id = view.getId();
        if (id == R.id.tvQuestion1) {//设置密码提示问题1
            showSetAnswerDialog(0);
        } else if (id == R.id.tvQuestion2) {//设置密码提示问题2
            showSetAnswerDialog(1);
        } else if (id == R.id.tvQuestion3) {//设置密码提示问题3
            showSetAnswerDialog(2);
        } else if (id == R.id.tvConfirm) {//确定
            String password = mDataBinding.etPassword.getText().toString().trim();
            String passwordAgain = mDataBinding.etPasswordAgain.getText().toString().trim();
            if (TextUtils.isEmpty(password)){
                ToastUtils.showShort(getString(R.string.please_input_password));
                return;
            }
            if (TextUtils.isEmpty(passwordAgain)){
                ToastUtils.showShort(getString(R.string.please_input_password_again));
                return;
            }
            if (!password.equals(passwordAgain)) {
                ToastUtils.showShort(getString(R.string.password_is_different));
                return;
            }
            if (TextUtils.isEmpty(question1)) {
                ToastUtils.showShort(getString(R.string.please_set_password_question));
                return;
            }
            SPUtil.putString(mContext, Content.myPassword, password);
            SPUtil.putString(mContext, Content.myQuestion1, question1);
            SPUtil.putString(mContext, Content.myAnswer1, answer1);
            SPUtil.putBoolean(mContext, Content.isHavePassword, true);
            if (!TextUtils.isEmpty(question2)) {
                SPUtil.putString(mContext, Content.myQuestion2, question2);
                SPUtil.putString(mContext, Content.myAnswer2, answer2);
            }
            if (!TextUtils.isEmpty(question3)) {
                SPUtil.putString(mContext, Content.myQuestion3, question3);
                SPUtil.putString(mContext, Content.myAnswer3, answer3);
            }
            startActivity(InputPwdActivity.class);
        }
    }

    @Override
    protected void initData() {

    }

    private void showSetAnswerDialog(int pos) {
        SetAnswerDialog dialog = new SetAnswerDialog(mContext);
        if (pos == 0) {
            dialog.setQuestion(question1);
            dialog.setAnswer(answer1);
        } else if (pos == 1) {
            dialog.setQuestion(question2);
            dialog.setAnswer(answer2);
        } else if (pos == 2) {
            dialog.setQuestion(question3);
            dialog.setAnswer(answer3);
        }
        dialog.setListener(new SetAnswerDialog.Ilistener() {
            @Override
            public void clickConfirm(String question, String answer) {
                if (pos == 0) {
                    question1 = question;
                    answer1 = answer;
                    mDataBinding.tvQuestion1.setText(getString(R.string.have_set));
                    mDataBinding.tvQuestion1.setTextColor(Color.parseColor("#1A1A1A"));
                } else if (pos == 1) {
                    question2 = question;
                    answer2 = answer;
                    mDataBinding.tvQuestion2.setText(getString(R.string.have_set));
                    mDataBinding.tvQuestion2.setTextColor(Color.parseColor("#1A1A1A"));
                } else if (pos == 2) {
                    question3 = question;
                    answer3 = answer;
                    mDataBinding.tvQuestion3.setText(getString(R.string.have_set));
                    mDataBinding.tvQuestion3.setTextColor(Color.parseColor("#1A1A1A"));
                }
            }
        });
        dialog.show();
    }

}